#include "GridCell.h"
#include "defineNotice.h"

CCRect GridCell::slicesSize = CCRect(0, 107, 117, 80);

GridCell::GridCell(void)
{
}


GridCell::~GridCell(void)
{
}


GridCell * GridCell::create()
{
    GridCell * pRet = new GridCell();

    if (pRet && pRet->init())
    {

    }
    return pRet;
}


bool GridCell::init()
{
    bool bRet = false;
    do
    {
        CC_BREAK_IF(! CCSprite::init());
        setContentSize( CCSize (slicesSize.size.width, slicesSize.size.height));
        susliksOffsetXY = CCPoint(ccp(0, 60));
        susliksSize = CCSize (118, 106);
        hitRect = NULL;
        addNotificationCenter();
       // initHitRect();
        initPotHoleBG();
        initSusliksAction();
        bRet = true;
    }
    while (0);
    return bRet;
}

void GridCell::addNotificationCenter()
{

}

void GridCell::onActionEnd(CCObject * value)
{
    sendCellActionEndNotifcation(NULL);
}

void GridCell::sendCellActionEndNotifcation(CCObject * value)
{
    CCNotificationCenter::sharedNotificationCenter()->postNotification(CELL_ACTION_END ,((CCObject *)location));
}

void GridCell::setLocation(CCPoint *  location)
{
    this->location = location;
}

CCPoint * GridCell::getLocation()
{
    return this->location;
}

void GridCell::setHitWin(bool isHitWin)
{
    if (isHitWin)
    {
        susliksAction->changeStatus(3);
    }
    else
    {
        //susliksAction->changeStatus(2);
    }
}

void GridCell::runningSusliks()
{
    susliksAction->changeStatus(1);
}

void GridCell::initHitRect()
{

    int width = this->getContentSize().width ;
    int height =  this->getContentSize().height;

    int tempWidth = width;
    int tempHeight = height;

    //if (width < susliksSize.width)
    //{
    //	hitRect.width += (width - susliksSize.width);
    //}

    if (height < susliksSize.height )
    {
        tempHeight =  susliksSize.height;
    }
    tempHeight += susliksOffsetXY.y;

    CCRect rect = this->boundingBox();
    hitRect = new CCRect(rect);
    hitRect->size.height = tempHeight;


	CCLayerColor * clc = CCLayerColor::create(ccc4(225, 225, 0 ,100));
	clc->setContentSize(hitRect->size);
	this->addChild(clc);
}

CCRect * GridCell::getHitRect()
{
	if (!hitRect)
	{
		 initHitRect();
	}
   


    return hitRect;
}

void GridCell::initPotHoleBG()
{
    CCSprite * bg = CCSprite::create("001.png", GridCell::slicesSize);
    this->addChild(bg, 0);
    bg->setPosition(ccp(this->getContentSize().width *0.5, this->getContentSize().height *0.5));
}

void GridCell::initSusliksAction()
{
    susliksAction = SusliksAction::create();
    this->addChild(susliksAction,1);
    susliksAction->setPosition(ccp(this->getContentSize().width *0.5, this->getContentSize().height *0.5 + susliksOffsetXY.y));
    susliksAction->getNotificationCenter()->addObserver(this, callfuncO_selector(GridCell::onActionEnd), SusliksAction::SUSLIKS_ACTION_END, NULL);
}
